package com.wrupple.muba.catalogs.client.widgets.editors.composite.delegates;

import org.cobogw.gwt.user.client.ui.Button;
import org.cobogw.gwt.user.client.ui.ButtonBar;

import com.google.gwt.cell.client.Cell;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.logical.shared.ValueChangeEvent;
import com.google.gwt.event.logical.shared.ValueChangeHandler;
import com.google.gwt.event.shared.HandlerRegistration;
import com.google.gwt.user.cellview.client.CellList;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.view.client.Range;
import com.google.gwt.view.client.SelectionChangeEvent;
import com.google.gwt.view.client.SelectionChangeEvent.Handler;
import com.google.gwt.view.client.SingleSelectionModel;
import com.wrupple.muba.catalogs.client.module.services.presentation.FilterableDataProvider;
import com.wrupple.muba.catalogs.client.widgets.editors.ForeignValueRelationEditor;
import com.wrupple.muba.catalogs.domain.JsCatalogKey;
import com.wrupple.muba.common.client.application.DataCallback;
import com.wrupple.muba.common.domain.FilterData;

public class ForeignValueRelationEditorImpl extends Composite implements
		ForeignValueRelationEditor {

	private RelationshipDelegate delegate;
	private FilterableDataProvider<JsCatalogKey> dataProvider;
	private SingleSelectionModel<JsCatalogKey> selectionModel;

	FilterData currentValue;
	private ButtonBar bottomPanel;
	CellList<JsCatalogKey> list;
	public ForeignValueRelationEditorImpl(RelationshipDelegate delegate,Cell<JsCatalogKey> cell,
			FilterableDataProvider<JsCatalogKey> dataProvider) {
		super();
		Button addButton = new Button("+", new AdditionClickHandler());
		Button removeButton = new Button("x");
		selectionModel = new SingleSelectionModel<JsCatalogKey>();
		selectionModel.addSelectionChangeHandler(new SelectionChangeHandler());
		 list= new CellList<JsCatalogKey>(cell);
		
		ButtonBar topPanel = new ButtonBar();
		bottomPanel = new ButtonBar();
		topPanel.add(addButton);
		bottomPanel.add(removeButton);

		list.setSelectionModel(selectionModel);

		VerticalPanel structure = new VerticalPanel();
		structure.add(topPanel);
		structure.add(list);
		structure.add(bottomPanel);
		bottomPanel.setVisible(false);
		initWidget(structure);
		
		this.delegate=delegate;
		this.dataProvider = dataProvider;
	}

	@Override
	public FilterData getValue() {
		return currentValue;
	}

	@Override
	public void setValue(FilterData value) {
		this.currentValue=value;
		dataProvider.setFilter(value);
		Range range= new Range(0,Integer.MAX_VALUE);
		list.setVisibleRangeAndClearData(range, true);
		dataProvider.addDataDisplay(list);
	}

	@Override
	public void setValue(FilterData value, boolean fireEvents) {
		setValue(value);
		if(fireEvents){
			ValueChangeEvent.fire(this, value);
		}
	}

	@Override
	public HandlerRegistration addValueChangeHandler(ValueChangeHandler<FilterData> handler) {
		return addHandler(handler, ValueChangeEvent.getType());
	}



	class SelectionChangeHandler implements Handler {

		@Override
		public void onSelectionChange(SelectionChangeEvent event) {
			JsCatalogKey selectedObject = selectionModel.getSelectedObject();
			
			bottomPanel.setVisible(selectedObject!=null);
		}

	}

	class AdditionClickHandler implements ClickHandler {

		@Override
		public void onClick(ClickEvent event) {
			delegate.onRelationshipAdditionRequested(currentValue, new ChangeValue());
		}

	}
	
	
	class RemovalClickHandler implements ClickHandler {

		@Override
		public void onClick(ClickEvent event) {
			JsCatalogKey selectedObject = selectionModel.getSelectedObject();
			if(selectedObject!=null){
				String valueToRemove = selectedObject.getId();
				delegate.onRelationshipRemovalRequested(currentValue, valueToRemove, new ChangeValue());
			}
		}
		
	}
	
	class ChangeValue extends DataCallback<FilterData>{

		@Override
		public void execute() {
			setValue(result,true);
		}
		
	}

}
